Thumb

What is LINQ?

1/12/2020 6:46:11 AM

Language Integrated Query (LINQ) is a query language designed by Microsoft as part of dot net 3.5 framework which write query in wide verity of data sources like Arrays, Collections, Database Tables, DataSet’s, Xml data etc. LINQ query look like SQL query but structure is different. Now given bellow the example of the code and explain the code: 

Structure: from <alias> in <collection | array> [clauses] select <alias>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using testForClass1;
namespace testFor
{
    public class Program
    {
        static void Main(string[] args)
        {
            //int array
            int[] myNum = { 10, 20, 30, 40, 50, 60, 70, 80 };
            //linq query
            var num = from als in myNum where als <= 40 select als;
            //print value
            foreach (var n in num)
            {
                Console.WriteLine(n);
            }
            Console.WriteLine();
            //string array
            string findName = "reza";
            string[] myStr = { "jesy", "reza", "tofile", };
            //linq query
            var name = from als in myStr where als == findName select als;
            //print the find name
            foreach (var nm in name)
            {
                Console.WriteLine("Find my name : " + nm);
            }
            Console.WriteLine();
            // List student object
            List<Student> studentList = new List<Student>() {
                new Student(){ StudentID=1, StudentName="Jesy"},
                new Student(){ StudentID=2, StudentName="Tofile"},
                new Student(){ StudentID=3, StudentName="Reza"},
                new Student(){ StudentID=4, StudentName="Sakib"},
                new Student(){ StudentID=5, StudentName="Farhan"}
            };
            // find the student by condition
            var findSpesiStdById = from als in studentList where als.StudentID >= 3 select als.StudentName;
            //print the find name
            foreach (var stdName in findSpesiStdById)
            {
                Console.WriteLine(stdName);
            }

            Console.Read();
        }
    }
    public class Student
    {
        public int StudentID { get; set; }
        public string StudentName { get; set; }
    }
}

In this code first find the integer values from integer array using LINQ then find string array from string name using LINQ then we create student class and under the class two property under the class and this class insert the value and create the list. Now find the specific student from the list by student id using LINQ.